<template>
	<view class="scope-float-btn-c">
		
		<view class="free-stock" v-if="stock" @tap="handleUseFreeTime">{{stock}}次机会</view>
		
	</view>
</template>

<script>
	import {
			mapGetters
		} from "vuex"
	export default {
		props: {
			uuid: String,
			nodeType: String
		},
		data () {
			return {
				inviteTotal: 0,
				usedTotal: 0, 
				stock: 0,
				title: ''
			}
		},
		computed: {
			
			...mapGetters(["userInfo"]),
			isAnimate () {
				return true
			}
		},
		created () {
			this.initData()
			
			uni.$on('refreshFreeTicketTotal', () => {
				this.initData()
			})
		},
		methods: {
			handleClick () {
				uni.navigateTo({
					url: '/pages/activityTicket/record?uuid=' + this.uuid + '&node_type=' + this.nodeType+'&type=share'
				})
				
				// 如果没有剩余次数，直接跳转到邀请记录
				// if (this.stock === 0) {
				// 	uni.navigateTo({
				// 		url: '/pages/activityTicket/record?uuid=' + this.uuid + '&node_type=' + this.nodeType
				// 	})
				// 	return false
				// }
				
				// uni.showModal({
				//     title: '选择操作',
				//     // content: '选择操作',
				// 	confirmText: '兑换1次',
				// 	cancelText: '邀请记录',
				//     success: (res) => {
				//         if (res.confirm) {
				// 			// 使用一次兑换记录
				//             this.$emit('useFreeTicket')
				//         } else if (res.cancel) {
				//             uni.navigateTo({
				//             	url: '/pages/activityTicket/record?uuid=' + this.uuid + '&node_type=' + this.nodeType
				//             })
				//         }
				//     }
				// })
			},
			handleUseFreeTime () {
				uni.showModal({
				    title: '操作提示',
				    content: '确定使用一次免费抽奖机会吗？',
					confirmText: '确定使用',
					cancelText: '暂不',
				    success: (res) => {
				        if (res.confirm) {
							// 使用一次兑换记录
				            this.$emit('useFreeTicket')
							
							setTimeout(() => {
								this.initData()
							}, 1000)
						}
				    }
				})
			},
			initData () {
				if (!this.uuid) {
					return false
				}
				if(this.userInfo&&this.userInfo.id){
					
				this.$http('/activity/ticket-total', 'GET', {
					node_type: this.nodeType,
					uuid: this.uuid
				}).then(res => {
					this.inviteTotal = res.data.invite_total
					this.usedTotal = res.data.used_total
					this.stock = res.data.stock
					this.$parent.set_ticket_id(res.data.ticket_id);
					this.title = res.data.title
				})
				}
			}
		},
		watch: {
			uuid (val) {
				this.initData()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scope-float-btn-c {
		position: fixed;
		right: 20rpx;
		bottom: 550rpx;
		width: 120rpx;
		z-index:9999;
	}
	
	
	.free-stock {
		background: white;
		border-radius: 30rpx;
		color: red;
		font-size: 22rpx;
		padding: 6rpx 10rpx;
		text-align: center;
		margin-top: 20rpx;
	}
</style>
